/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hhf.resourceBundle;

import java.lang.reflect.Field;
import java.util.ResourceBundle;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Inject;
import org.slf4j.Logger;

/**
 *
 * @author François
 */
public class BundleProducer {
	@Inject
	Logger logger;

	@Produces
	public ResourceBundle getResourceBundle(InjectionPoint ip) {
		Class c = ip.getMember().getDeclaringClass();
		String bundleName = c.getName();
		String fieldName =  ip.getMember().getName();
		ResourceBundle bundle = null;
		try {
			Field f = c.getDeclaredField(fieldName);
			if(f.isAnnotationPresent(BundleName.class)) {
				BundleName ln = f.getAnnotation(BundleName.class);
				bundleName = ln.value();
				logger.info("Injection du bundle : {}", bundleName);
				bundle = ResourceBundle.getBundle(bundleName);
			}
		} catch (NoSuchFieldException ex) {
		} catch (SecurityException ex) {
		}
		return bundle;
	}
	
}
